package com.liuxu.topnews.floating;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.liuxu.topnews.BuildConfig;
import com.liuxu.topnews.R;
import com.liuxu.topnews.acitivity.HomeFragment;
import com.liuxu.topnews.acitivity.MainActivity;
import com.liuxu.topnews.utils.ToastUtils;

import static com.liuxu.topnews.MyApplication.Pause;
import static com.liuxu.topnews.MyApplication.permission;


public class FloatingView extends LinearLayout {
    //记录小窗口的宽度
    public static int viewWidth;
    //记录小窗口的高度
    public static int viewHeight;
    //记录系统状态栏高度
    private static int statusBarHeight;
    //用于更新小悬浮窗位置
    private WindowManager windowManager;
    //小悬浮窗的参数
    private WindowManager.LayoutParams mParams;
    //记录当前手指在屏幕上的横坐标值
    private float xInScreen;
    //记录当前手指在屏幕上的纵坐标值
    private float yInScreen;
    //记录手指在屏幕上按下的横坐标
    private float xDownInScreen;
    //记录手指在屏幕上按下的纵坐标
    private float yDownInScreen;
    //记录手指按下时小悬浮窗的横坐标
    private float xInView;
    //记录手指按下时小悬浮窗的纵坐标
    private float yInView;
    private Context mcontext;
    private ImageButton controlButton;
    private ImageButton returnButton;
    private TextView now_read_app_name;
    private TextView now_total_number;
    private TextView now_page_number;

    public FloatingView(Context context) {
        super(context);
        mcontext=context;
        windowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        switch (BuildConfig.version) {
            case "normal":
                LayoutInflater.from(context).inflate(R.layout.float_window, this);
                break;
            case "xm":
                LayoutInflater.from(context).inflate(R.layout.float_window_hw, this);
                break;
            case "hw":
                LayoutInflater.from(context).inflate(R.layout.float_window_hw, this);
                break;
            default:
                LayoutInflater.from(context).inflate(R.layout.float_window, this);
                break;
        }
        View view=findViewById(R.id.small_window_layout);
        viewHeight=view.getLayoutParams().height;
        viewWidth=view.getLayoutParams().width;
        now_read_app_name=(TextView) findViewById(R.id.now_read_app_name);
        now_total_number=(TextView) findViewById(R.id.now_total_number);
        now_page_number=(TextView) findViewById(R.id.now_page_number);
        controlButton=(ImageButton)findViewById(R.id.control);
//        returnButton=(ImageButton)findViewById(R.id.returnButton);
        now_read_app_name.setText("未开始");
        now_total_number.setText("无数据");
        now_page_number.setText("无数据");
        changeimage();
        initListener();
    }
    private void initListener() {
        controlButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (permission){
                    changeimage();
                    Pause=!Pause;
                }else {
                    ToastUtils.getInstance().show("请在应用内点击执行按钮！");
                }
            }
        });
//        returnButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(getContext(), MainActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
//                getContext().startActivity(intent);
//            }
//        });
    }
    private void changeimage(){
        if (Pause||!permission){
            controlButton.setImageDrawable(ContextCompat.getDrawable(mcontext,R.drawable.begin));
        }else{
            controlButton.setImageDrawable(ContextCompat.getDrawable(mcontext,R.drawable.pause));
        }

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //手指按下时记录必要的数据  纵坐标的值都需要减去状态栏的高度
                xInView=event.getX();
                yInView=event.getY();  //触摸点相对于控件的左上角位置
                xDownInScreen=event.getRawX();   //触摸点相对于这个屏幕左上角位置
                yDownInScreen=event.getRawY();
                xInScreen=event.getRawX();
                yInScreen=event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                xInScreen=event.getRawX();
                yInScreen=event.getRawY();
                //手指移动时更新悬浮窗的位置
                updateViewPosition();
                break;
            case MotionEvent.ACTION_UP:
                //如果手指离开屏幕  xDownInScreen和xInScreen相同 且 yDownInScreen和yInScreen相等,则视为触发了单机事件
                if(xDownInScreen==xInScreen&&yDownInScreen==yInScreen){
//                    openBigWindow();
                }
                break;
            default:
                break;
        }
        return true;
    }

    private void updateViewPosition(){
        //在移动的过程中  需要减去触摸点相对于控件的位置   否则就会出现移动时 窗口跟着左上角走, 而不是正中间
        mParams.x=(int) (xInScreen-xInView);
        mParams.y=(int) (yInScreen-yInView);
        windowManager.updateViewLayout(this, mParams);
    }

    public void updateData(String appname,String total_time,String time){
        now_read_app_name.setText(appname);
        now_total_number.setText(total_time);
        now_page_number.setText(time);
        changeimage();
        windowManager.updateViewLayout(this, mParams);
    }

    public void setmParams(WindowManager.LayoutParams mParams) {
        this.mParams = mParams;
    }
}
